package com.webinsky.web.resource;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.webinsky.article.domain.MisArticle;
import com.webinsky.article.domain.MisRelationship;
import com.webinsky.article.domain.MisRelationshipType;
import com.webinsky.article.service.ArticleManagement;
import com.webinsky.article.service.RelationshipManagement;
import com.webinsky.common.proj.Lang;
import com.webinsky.folder.domain.Folder;
import com.webinsky.folder.domain.FolderArticle;
import com.webinsky.folder.domain.FolderFilter;
import com.webinsky.folder.domain.FolderIndex;
import com.webinsky.folder.domain.FolderIndexCount;
import com.webinsky.folder.domain.FolderIndexCounts;
import com.webinsky.folder.domain.FolderType;
import com.webinsky.folder.domain.Folders;
import com.webinsky.folder.service.FolderManagement;
import com.webinsky.publish.service.InfoManagement;
import com.webinsky.web.data.ConsoleCountForm;
import com.webinsky.web.data.FolderAddArticleForm;
import com.webinsky.web.data.FolderArticlePositionForms;
import com.webinsky.web.data.FolderArticlesForm;
import com.webinsky.web.data.FolderChangeNameForm;
import com.webinsky.web.data.FolderCreateForm;
import com.webinsky.web.data.FolderCreateForms;
import com.webinsky.web.data.FolderDeleteArticleForm;
import com.webinsky.web.data.FolderDeleteForm;
import com.webinsky.web.data.FolderGetForm;
import com.webinsky.web.data.FolderLangDeleteForm;
import com.webinsky.web.data.FolderLangGetForm;
import com.webinsky.web.data.FolderPositionForms;
import com.webinsky.web.data.Response;

import reactor.core.publisher.Mono;

@Component
public class FolderResource {
	
	private static final Logger logger = LoggerFactory.getLogger(FolderResource.class);
	
	@Autowired
	private FolderManagement folderManagement;
	@Autowired
	private ArticleManagement articleManagement;
	@Autowired
	private RelationshipManagement relationshipManagement;
	@Autowired
	private InfoManagement infoMangement;
	
	private Folder convert(FolderCreateForm requestForm){
		Folder folder = new Folder();
		folder.setId(requestForm.id);
		folder.setName(requestForm.name);
		folder.setType(FolderType.parse(requestForm.type));
		folder.setSort(requestForm.sort);
		folder.setSiteId(Integer.valueOf(requestForm.siteId));
		folder.setLanguage(Lang.parse(requestForm.lang));
		if ( requestForm.type == FolderType.Category.getType() ) {
			folder.setItemId(requestForm.itemId);
		}
		return folder;
	}
	
	
	/**
	 * 取得目录接口
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> getBookFolders(ServerRequest req){
		return req.bodyToMono(FolderGetForm.class).flatMap(form -> {
			FolderFilter filter = new FolderFilter();
			try {
				if ( form.name != null ){
					filter.setFolderName(URLDecoder.decode(form.name, "utf-8"));
				}
			} catch (Exception e) {
				logger.error("[getBookFolders] decoding failed!", e);
			}
			if ( form.folderType != null )
				filter.setType(FolderType.parse(form.folderType));
			filter.setStart(0);
			filter.setSize(9999);
			filter.setSiteId(Integer.valueOf(form.siteId));
			filter.setLanguage(form.lang);
			Folders folders = folderManagement.getFoldersByFilter(filter);
			if (form.itemId > 0) {
				folders.setFolders(
					folders.getFolders().stream().filter(m ->
							m.getItemId() == form.itemId
					).collect(Collectors.toList())
				);
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(folders, Response.STATUS_OK, ""));
		});
	}
	
	/**
	 * 新建目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> addFolder(ServerRequest req){
		return req.bodyToMono(FolderCreateForm.class).flatMap(form -> {
			Folder folder = convert(form);
			folderManagement.addFolder(folder);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(folder, Response.STATUS_OK, ""));
		});
	}
	
	public Mono<ServerResponse> addCatalogs(ServerRequest req){
		return req.bodyToMono(FolderCreateForms.class).flatMap( data -> {
			if ( data == null || data.cnForm == null || data.enForm == null || data.jpForm == null || data.krForm == null || data.krForm == null || data.thForm == null || data.twForm == null ){
				return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "参数为空"));
			}
			Integer siteId = Integer.valueOf(data.siteId);
			if ( siteId != null ){
				// 如果是要更新
				List<Folder> folders = new ArrayList<>();
				if ( data.cnForm != null ){
					Folder folder = convert(data.cnForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				if ( data.jpForm != null ){
					Folder folder = convert(data.jpForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				if ( data.enForm != null ){
					Folder folder = convert(data.enForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				if ( data.krForm != null ){
					Folder folder = convert(data.krForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				if ( data.thForm != null ){
					Folder folder = convert(data.thForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				if ( data.twForm != null ){
					Folder folder = convert(data.twForm);
					folder.setType(FolderType.Category);
					folders.add(folder);
				}
				
				if ( data.cnForm.id > 0 ) {	
					folderManagement.updateFolders(folders);
					return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
				}
				
				folderManagement.addFolders(folders, siteId, FolderType.Category);
				// 新建relationship
				MisRelationship misRelationship = new MisRelationship();
				misRelationship.setSiteId(siteId);
				folders.forEach(f -> {
					switch ( f.getLanguage() ){
					case jaJP:
						misRelationship.setJpId(f.getId());
						break;
					case zhCN:
						misRelationship.setCnId(f.getId());
						break;
					case enUS:
						misRelationship.setEnId(f.getId());
						break;						
					case zhTW:
						misRelationship.setTwId(f.getId());
						break;						
					case ko:
						misRelationship.setKrId(f.getId());
						break;						
					case th:
						misRelationship.setThId(f.getId());
						break;
					default:
					}
				});				
				misRelationship.setType(MisRelationshipType.Catalog);
				relationshipManagement.create(misRelationship);
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	
	public Mono<ServerResponse> addFolders(ServerRequest req){
		return req.bodyToMono(FolderCreateForms.class).flatMap(data -> {
			if ( data == null || data.cnForm == null || data.enForm == null || data.jpForm == null || data.krForm == null || data.thForm == null || data.twForm == null ){
				return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "参数为空"));
			}
			Integer siteId = Integer.valueOf(data.siteId);
			FolderType folderType = FolderType.parse(data.type);
			if ( siteId != null && folderType != null ){
				// 更新
				List<Folder> folders = new ArrayList<>();
				if ( data.cnForm != null ){
					Folder folder = convert(data.cnForm);
					folders.add(folder);
				}
				if ( data.jpForm != null ){
					Folder folder = convert(data.jpForm);
					folders.add(folder);
				}
				if ( data.enForm != null ){
					Folder folder = convert(data.enForm);
					folders.add(folder);
				}
				if ( data.krForm != null ){
					Folder folder = convert(data.krForm);
					folders.add(folder);
				}
				if ( data.thForm != null ){
					Folder folder = convert(data.thForm);
					folders.add(folder);
				}
				if ( data.twForm != null ){
					Folder folder = convert(data.twForm);
					folders.add(folder);
				}
				
				
				if ( data.cnForm.id > 0 ) {	
					folderManagement.updateFolders(folders);
					return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
				}
				
				// 新建
				folderManagement.addFolders(folders, siteId, folderType);
				// 新建relationship
				if ( folderType == FolderType.Category || folderType == FolderType.Item){
					MisRelationship misRelationship = new MisRelationship();
					misRelationship.setSiteId(siteId);
					folders.forEach(f -> {
						switch ( f.getLanguage() ){
						case jaJP:
							misRelationship.setJpId(f.getId());
							break;
						case zhCN:
							misRelationship.setCnId(f.getId());
							break;
						case enUS:
							misRelationship.setEnId(f.getId());
							break;						
						case zhTW:
							misRelationship.setTwId(f.getId());
							break;						
						case ko:
							misRelationship.setKrId(f.getId());
							break;						
						case th:
							misRelationship.setThId(f.getId());
							break;
						default:
						}
					});				
					if ( folderType == FolderType.Category){
						misRelationship.setType(MisRelationshipType.Catalog);
					} else {
						misRelationship.setType(MisRelationshipType.Item);
					}
					relationshipManagement.create(misRelationship);
				}
			}
			
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}


	/**
	 * 取得多语言目录接口
	 */
	public Mono<ServerResponse> getFolders(ServerRequest req){
		return req.bodyToMono(FolderLangGetForm.class).flatMap(form -> {
			Integer siteId = Integer.valueOf(form.siteId);
			if ( form.folderType == 0 ){
				List<MisRelationship> itemShips = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Item);
				if ( itemShips.isEmpty() ){
					return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
				}
				List<Integer> itemCatalogIds = new ArrayList<>();
				itemShips.forEach(itemShip -> {
					if ( itemShip.getCnId() == form.id ){
						itemCatalogIds.add(itemShip.getCnId());
						if ( itemShip.getJpId() > 0 ){
							itemCatalogIds.add(itemShip.getJpId());
						}
						if ( itemShip.getEnId() > 0 ){
							itemCatalogIds.add(itemShip.getEnId());
						}
						if ( itemShip.getKrId() > 0 ){
							itemCatalogIds.add(itemShip.getKrId());
						}
						if ( itemShip.getThId() > 0 ){
							itemCatalogIds.add(itemShip.getThId());
						}
						if ( itemShip.getTwId() > 0 ){
							itemCatalogIds.add(itemShip.getTwId());
						}
					}
				});
				if ( itemCatalogIds.isEmpty() ) {
					itemCatalogIds.add(form.id);
				}
				Folders itemFolders = folderManagement.getFoldersByIds(itemCatalogIds, FolderType.Item);
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(itemFolders.getFolders(), Response.STATUS_OK, ""));
			} else if ( form.folderType == 1 ){
				List<MisRelationship> catalogShips = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Catalog);
				if ( catalogShips.isEmpty() ){
					return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
				}
				List<Integer> catalogIds = new ArrayList<>();
				catalogShips.forEach( catalogShip -> {
					if ( catalogShip.getCnId() == form.id ){
						catalogIds.add(catalogShip.getCnId());
						if ( catalogShip.getJpId() > 0 ){
							catalogIds.add(catalogShip.getJpId());
						}
						if ( catalogShip.getEnId() > 0 ){
							catalogIds.add(catalogShip.getEnId());
						}
						if ( catalogShip.getKrId() > 0 ){
							catalogIds.add(catalogShip.getKrId());
						}
						if ( catalogShip.getThId() > 0 ){
							catalogIds.add(catalogShip.getThId());
						}
						if ( catalogShip.getTwId() > 0 ){
							catalogIds.add(catalogShip.getTwId());
						}
					}
				});
				if ( catalogIds.isEmpty() ) {
					catalogIds.add(form.id);
				}
				Folders catalogFolders = folderManagement.getFoldersByIds(catalogIds, FolderType.Category);
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalogFolders.getFolders(), Response.STATUS_OK, ""));
			} else {
				List<MisRelationship> itemShips = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Item);
				if ( itemShips.isEmpty() ){
					return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
				}
				List<Integer> itemCatalogIds = new ArrayList<>();
				itemShips.forEach(itemShip -> {
					if ( itemShip.getCnId() == form.id ){
						itemCatalogIds.add(itemShip.getCnId());
						if ( itemShip.getJpId() > 0 ){
							itemCatalogIds.add(itemShip.getJpId());
						}
						if ( itemShip.getEnId() > 0 ){
							itemCatalogIds.add(itemShip.getEnId());
						}
						if ( itemShip.getKrId() > 0 ){
							itemCatalogIds.add(itemShip.getKrId());
						}
						if ( itemShip.getThId() > 0 ){
							itemCatalogIds.add(itemShip.getThId());
						}
						if ( itemShip.getTwId() > 0 ){
							itemCatalogIds.add(itemShip.getTwId());
						}
					}
				});
				if ( itemCatalogIds.isEmpty() ) {
					itemCatalogIds.add(form.id);
				}
				Folders itemFolders = folderManagement.getFoldersByIds(itemCatalogIds, FolderType.Item);
				List<MisRelationship> catalogShips = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Catalog);
				if ( catalogShips.isEmpty() ){
					return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
				}
				List<Integer> catalogIds = new ArrayList<>();
				catalogShips.forEach( catalogShip -> {
					if ( catalogShip.getCnId() == form.id ){
						catalogIds.add(catalogShip.getCnId());
						if ( catalogShip.getJpId() > 0 ){
							catalogIds.add(catalogShip.getJpId());
						}
						if ( catalogShip.getEnId() > 0 ){
							catalogIds.add(catalogShip.getEnId());
						}
						if ( catalogShip.getKrId() > 0 ){
							catalogIds.add(catalogShip.getKrId());
						}
						if ( catalogShip.getThId() > 0 ){
							catalogIds.add(catalogShip.getThId());
						}
						if ( catalogShip.getTwId() > 0 ){
							catalogIds.add(catalogShip.getTwId());
						}
					}
				});
				if ( catalogIds.isEmpty() ) {
					catalogIds.add(form.id);
				}
				Folders catalogFolders = folderManagement.getFoldersByIds(catalogIds, FolderType.Category);
				List<Folder> folders = new ArrayList<>();
				folders.addAll(itemFolders.getFolders());
				folders.addAll(catalogFolders.getFolders());
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(folders, Response.STATUS_OK, ""));
			}
			
		});
	}

	/**
	 * 删除多语言目录接口
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> delFolder(ServerRequest req){
		return req.bodyToMono(FolderLangDeleteForm.class).flatMap(form -> {
			FolderType type = FolderType.parse(form.type);
			if ( type != null ){
				form.id.forEach(e -> {
					folderManagement.deleteFolder(e, type);
				});
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));	
			}
			return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
		});
	}
	
	/**
	 * 修改文件目录名字
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> changeFolderName(ServerRequest req){
		return req.bodyToMono(FolderChangeNameForm.class).flatMap(form -> {
			// TODO 缺少排序?
			folderManagement.changeFolderName(form.id, FolderType.parse(form.type), form.name);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	/**
	 * 删除目录夹
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> deleteFolder(ServerRequest req){
		return req.bodyToMono(FolderDeleteForm.class).flatMap(form -> {
			folderManagement.deleteFolder(form.id, FolderType.parse(form.type));
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	/**
	 * 添加文章到目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> addArticleToFolder(ServerRequest req){
		return req.bodyToMono(FolderAddArticleForm.class).flatMap(form -> {
			List<FolderArticle> articles = new ArrayList<>();
			if ( form.articles != null ){
				form.articles.forEach(a -> {
					FolderArticle fa = new FolderArticle();
					fa.setArticleId(a.articleId);
					fa.setSort(a.sort);
					fa.setSiteId(Integer.valueOf(form.siteId));
					fa.setLang(Lang.parse(form.lang));
					fa.setType(FolderType.parse(form.folderType));
					articles.add(fa);
				});
			}
			try {
				folderManagement.addArtileToFolder(form.folderId, FolderType.parse(form.folderType), articles);	
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(articles, Response.STATUS_OK, ""));
			} catch (Exception e){
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, e.getMessage()));
			}
		});
	}
	
	/**
	 * 删除某个文件夹下面的文章
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> deleteArticleFromFolder(ServerRequest req){
		return req.bodyToMono(FolderDeleteArticleForm.class).flatMap(form -> {
			FolderType type = FolderType.parse(form.folderType);				
			folderManagement.deleteArticleFromFolder(form.folderId, type, form.articleIds);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}

	public Mono<ServerResponse> getItemArticles(ServerRequest req){
		return req.bodyToMono(FolderArticlesForm.class).flatMap(form -> {
			if ( form.itemId <= 0 ) {
				return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, "item_id is not validate"));
			}
			List<FolderIndex> list = folderManagement.getFolderIndex(Integer.valueOf(form.siteId), Lang.parse(form.lang));
			List<Integer> catalogIds = folderManagement.getCatalogIdByItemId(form.itemId, Lang.parse(form.lang), Integer.valueOf(form.siteId));
			// 取得文章内容
			if ( list.isEmpty() ){
				return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, "data is empty"));
			}
			list = list.stream().filter(e -> {
				if ( !catalogIds.isEmpty() ) {
					if ( catalogIds.contains(e.getFolderId()) && e.getType() == FolderType.Category ) {
						return true;
					}
				}
				
				if ( form.itemId > 0 ) {
					if ( e.getFolderId() != form.itemId || e.getType() != FolderType.Item) {
						return false;
					}
				}
				return true;
			}).collect(Collectors.toList());
			List<FolderArticle> articles = new ArrayList<>();
			list.forEach(e -> {
				if ( e.getArticles() != null ){
					articles.addAll(e.getArticles());
				}
			});
			// 因为文章信息属于article service,所以要在wrapper service层查到所有信息
			List<Integer> articlesIds = articles.stream().map(e -> e.getArticleId()).collect(Collectors.toList());
			List<Integer> distinctIds = articlesIds.stream().distinct().collect(Collectors.toList());
			boolean isDetail = true;
			List<MisArticle> misArticles = articleManagement.getArticles(distinctIds, isDetail);
			// 文章的mapping关系
			Map<Integer, MisArticle> misArticlesMap = misArticles.stream().collect(Collectors.toMap(MisArticle::getId, Function.identity()));
			list.forEach(l -> {
				if ( l.getArticles() != null ) {
					l.getArticles().forEach(a -> {
						if ( misArticlesMap.get(a.getArticleId()) != null ) {
							a.setArticleTile(misArticlesMap.get(a.getArticleId()).getTitle());
							a.setArticleNo(misArticlesMap.get(a.getArticleId()).getArticleNo());
							if ( misArticlesMap.get(a.getArticleId()).getTags() != null && !misArticlesMap.get(a.getArticleId()).getTags().isEmpty() ) {
								misArticlesMap.get(a.getArticleId()).getTags().forEach(e -> a.getTags().add(String.valueOf(e.getTagId())));
							}
						}
					});
				}
			});
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(list, Response.STATUS_OK, ""));
		}).onErrorResume( t -> {
			return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, t.getMessage()));
		});
	}
	
	
	/**
	 * 获取所有文章
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> getArticles(ServerRequest req){
		return req.bodyToMono(FolderArticlesForm.class).flatMap(form -> {

			// TODO 新增检索条件 itemId, 为 0 时忽略 （已经写好要测试)
			// TODO 新增检索条件 categoryId, 为 0 时忽略（已经写好要测试)

			List<FolderIndex> list = folderManagement.getFolderIndex(Integer.valueOf(form.siteId), Lang.parse(form.lang));
			// 取得文章内容
			if ( !list.isEmpty() ){
				list = list.stream().filter(e -> {
					if ( form.categoryId > 0 ) {
						if ( e.getFolderId() != form.categoryId || e.getType() != FolderType.Category) {
							return false;
						}
					}
					else if ( form.itemId > 0 ) {
						if ( e.getFolderId() != form.itemId || e.getType() != FolderType.Item) {
							return false;
						}
					}
					return true;
				}).collect(Collectors.toList());
				List<FolderArticle> articles = new ArrayList<>();
				list.forEach(e -> {
					if ( e.getArticles() != null ){
						articles.addAll(e.getArticles());
					}
				});
				// 因为文章信息属于article service,所以要在wrapper service层查到所有信息
				List<Integer> articlesIds = articles.stream().map(e -> e.getArticleId()).collect(Collectors.toList());
				List<Integer> distinctIds = articlesIds.stream().distinct().collect(Collectors.toList());
				boolean isDetail = true;
				List<MisArticle> misArticles = articleManagement.getArticles(distinctIds, isDetail);
				// 文章的mapping关系
				Map<Integer, MisArticle> misArticlesMap = misArticles.stream().collect(Collectors.toMap(MisArticle::getId, Function.identity()));
				list.forEach(l -> {
					if ( l.getArticles() != null ) {
						l.getArticles().forEach(a -> {
							if ( misArticlesMap.get(a.getArticleId()) != null ) {
								a.setArticleTile(misArticlesMap.get(a.getArticleId()).getTitle());
								a.setArticleNo(misArticlesMap.get(a.getArticleId()).getArticleNo());
								if ( misArticlesMap.get(a.getArticleId()).getTags() != null && !misArticlesMap.get(a.getArticleId()).getTags().isEmpty() ) {
									misArticlesMap.get(a.getArticleId()).getTags().forEach(e -> a.getTags().add(String.valueOf(e.getTagId())));
								}
							}
						});
					}
				});
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(list, Response.STATUS_OK, ""));
		});
	}

	/**
	 * 移动文章位置
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> redirectArticlePosition(ServerRequest req){
		return req.bodyToMono(FolderArticlePositionForms.class).flatMap(forms -> {
			if ( forms != null ){
				forms.forms.forEach(form -> folderManagement.moveArtilePos(Integer.valueOf(form.siteId), form.arctileId, form.sort, FolderType.parse(form.folderType), form.folderId));
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}

	/**
	 * 移动目录位置
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> redirectFolderPosition(ServerRequest req){
		return req.bodyToMono(FolderPositionForms.class).flatMap(forms -> {
			if ( forms != null && forms.forms != null ) {
				forms.forms.forEach(form -> folderManagement.moveFolderPos(form.folderId, form.sort, FolderType.parse(form.folderType)));
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	
	/**
	 * 控制台count
	 * 
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> getAllArticlesCount(ServerRequest req){
		return req.bodyToMono(ConsoleCountForm.class).flatMap( data -> {
			FolderIndexCounts counts = folderManagement.getAllIndexes(Lang.parse(data.lang), data.siteId);
			int count = infoMangement.getCountBySiteId(data.siteId);
			FolderIndexCount fi = new FolderIndexCount();
			fi.setCount(count);
			fi.setFolderName("咨询");
			fi.setType(FolderType.Item);
			counts.getCounts().add(fi);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(counts, Response.STATUS_OK, ""));
		}).onErrorResume( t -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, t.getMessage())));
	}
	
}
